# -*- coding:utf-8 -*-
import random
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from spider.common.util.TimeUtil import TimeUtil


class SeleniumUtils():

    # chrome_path:浏览器路径
    # headless_flag:是否隐藏浏览器 TRUE:是 FALSE:否
    # is_max_mize:窗口是否最大化 TRUE:是 FALSE:否
    def __init__(self,chrome_path=None,headless_flag=True,is_max_mize=True):
        # 配置Chrome选项
        chrome_options = Options()
        if headless_flag==True:
            chrome_options.add_argument("--headless")
        if chrome_path!=None:
            chrome_options.binary_location = chrome_path  # 设置浏览器二进制文件位置
        if is_max_mize == True:
            self.browser=browser = webdriver.Chrome(options=chrome_options)
        self.browser.maximize_window()
        self.timeUtil=TimeUtil()

    def open_url(self, URL, max_value=0,min_value=0):
        self.browser.get(URL)
        # 等待body加载完成
        wait = WebDriverWait(self.browser, 10)
        wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        if min_value>max_value:
            max_value,min_value=min_value,max_value
        self.timeUtil.sleep_period_time(min_value=min_value, max_value=max_value)

    def source_code(self, fast_falg=True):
        # 等待body加载完成
        wait = WebDriverWait(self.get_brower(), 10)
        wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        time.sleep(0.3)
        driver_page_source = self.get_brower().page_source
        if not fast_falg:
            self.timeUtil.sleep_period_time(min_value=3, max_value=6)
        return driver_page_source


    def get_brower(self):
        return self.browser

    def quit(self):
        self.browser.quit()

    def find_element(self,xpath,index=0):
        elements = self.find_elements(xpath=xpath)
        if elements==None or len(elements)==0:
            return
        return elements[index]

    def find_elements(self, xpath):
        elements = self.get_brower().find_elements(By.XPATH, xpath)
        return elements

    #移动并点击该元素中间位置
    def move_and_clink_center(self, element, fast_falg=False, view_falg=True):
        if view_falg:
            # 将逛遍移动到可视区域
            self.get_brower().execute_script("arguments[0].scrollIntoView(true);", element)
        # 创建ActionChains对象
        # 获取input元素的宽度和高度
        width = element.size['width']
        height = element.size['height']
        # 计算input元素中心位置的偏移量
        offset_x = width / 2
        offset_y = height / 2

        # 将鼠标移动到input元素的中心位置
        actions = ActionChains(self.get_brower())
        actions.move_to_element_with_offset(element, offset_x, offset_y)
        time.sleep(0.5)
        # 执行点击动作
        actions.click()
        # 执行所有的操作
        actions.perform()
        wait = WebDriverWait(self.get_brower(), 10)
        wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
        time.sleep(0.5)
        if not fast_falg:
            self.timeUtil.sleep_period_time(min_value=3, max_value=6)

    def move_and_clink_xpath(self,xpath,fast_falg=False,index=0):
        element = self.get_brower().find_elements(By.XPATH, xpath)[index]
        return self.move_and_clink_center(element=element,fast_falg=fast_falg)


    #滑动到页面底部,一次性
    def scoll_low(self,fast_falg=True):
        # 滑动到页面底部
        self.get_brower().execute_script("window.scrollTo(0, document.body.scrollHeight);")
        # self.timeUtil.sleep_period_time(min_value=1, max_value=2)
        if not fast_falg:
            self.timeUtil.sleep_period_time(min_value=3, max_value=6)

    def input_text(self,xpath,text='',clearFalg=True,index=0,fast_falg=True):
        element = self.get_brower().find_elements(By.XPATH, xpath)[index]
        if clearFalg:
            element.clear()
        element.send_keys(text)
        # self.timeUtil.sleep_period_time(min_value=1, max_value=2)
        if not fast_falg:
            self.timeUtil.sleep_period_time(min_value=3, max_value=6)


